import bpy
import ctypes
import initLIB.load_dll as dllAPI


class C_MBTENINE_场景添加_Operator(bpy.types.Operator):
	bl_idname = "scene.game_scene_add"
	bl_label = "添加游戏场景"

	def execute(self, context):
		bpy.ops.scene.new()
		bpy.context.scene.update()
		bpy.context.scene.render.engine = 'mbt_game_renderer'

		dllAPI.flexCAPI.f_add场景(ctypes.c_wchar_p(bpy.context.scene.name))
		
		return {'FINISHED'}


class C_MBTENINE_场景更新到引擎_Operator(bpy.types.Operator):
	bl_idname = "scene.game_scene_add"
	bl_label = "场景更新到引擎"

	def execute(self, context):
		bpy.ops.scene.new()
		bpy.context.scene.update()
		bpy.context.scene.render.engine = 'mbt_game_renderer'

		dllAPI.flexCAPI.f_add场景(ctypes.c_wchar_p(bpy.context.scene.name))
		
		return {'FINISHED'}


class C_MBTENINE_场景图层更新到引擎_Operator(bpy.types.Operator):
	bl_idname = "scene.game_scene_layer_update"
	bl_label = '场景图层更新到引擎'

	def execute(self, context):
		渲染图层组 = context.scene.render.layers
		当前图层ID = 渲染图层组.active_index

		图层大小 = 渲染图层组[0].prop_单元大小
		#设置其他图层视距
		图层数量 = len(渲染图层组)
		
		for i in range(图层数量):
			
			渲染图层组[i].prop_单元大小 = int(图层大小)
			渲染图层组[i].name = '场景图层:  ' + str(图层大小) + 'X' + str(图层大小)
			图层大小 /= 2

			

		sceneName = ctypes.c_wchar_p(context.scene.name)

		当前场景渲染层数量 = dllAPI.flexCAPI.f_getScene图层数量(sceneName)
		print('render layer num = ', 当前场景渲染层数量)

		for i in range(len(渲染图层组)):
			视距 = 渲染图层组[i].prop_单元大小
			if i >= 当前场景渲染层数量:
				dllAPI.flexCAPI.f_add场景图层(sceneName, ctypes.c_int32(视距))
			else:
				dllAPI.flexCAPI.f_set场景图层视距(sceneName, ctypes.c_int32(i), ctypes.c_int32(视距))
				
			

		return {'FINISHED'}





classes = (
	C_MBTENINE_场景添加_Operator,
	C_MBTENINE_场景图层更新到引擎_Operator,
)


def register():
	for cls in classes:
		bpy.utils.register_class(cls)

def unregister():
	for cls in classes:
		bpy.utils.unregister_class(cls)

